/*
 * 
 * author     date                    function                 purpose
 * wuwentao   20111122             base structure            realize animation function*/


package com.badlogicgames.framwork;

import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class Animation {
	public static final int ANIMATION_LOOPING = 0;
	public static final int ANIMATION_NONLOOPING = 1;
	public static int loopCount = 0;
	public static boolean animationStop = false;
	public static boolean scrollStop = false;
	final TextureRegion[] keyFrames;
	final float frameDuration;
	public static float tmpFrameDuration = 0.02f;
	public static int gap;
	
	public static void variableReset()
	{
		scrollStop = false;
		animationStop = false;
		tmpFrameDuration = 0.02f;
		loopCount = 0;
		gap = 0;
	}
	
	public Animation (float frameDuration, TextureRegion... keyFrames) {
		this.frameDuration = frameDuration;
		this.keyFrames = keyFrames;
	}

	public TextureRegion getLoopKeyFrame(float stateTime)
	{
		int frameNumber = (int)(stateTime / frameDuration);
		frameNumber = frameNumber % keyFrames.length;		
		return keyFrames[frameNumber];
	}
	
	public TextureRegion getKeyFrame (float stateTime, int mode) {//mode should be set to 0 now
		
		int frameNumber = (int)(stateTime / tmpFrameDuration);

		if (mode == ANIMATION_NONLOOPING) {
			frameNumber = Math.min(keyFrames.length - 1, frameNumber);
		} else {
			frameNumber = (frameNumber-gap) % keyFrames.length;
		}
		System.out.println("frameNumber is: "+ frameNumber);
		System.out.println("loopCout is: "+ loopCount);
		if(mode == ANIMATION_LOOPING)
		{
			if(loopCount >= keyFrames.length)
			{
				frameNumber = 0;
				animationStop = true;
			}
			else if(loopCount==0 && frameNumber !=0)
			{
				frameNumber = 0;
			}
			else if(loopCount <=keyFrames.length-1)
			{
				if(loopCount == 1)
				{
					if(frameNumber != 1 && frameNumber !=0)
					{
						gap = frameNumber -1;
						frameNumber = 1;
					}
				}
				
				tmpFrameDuration = frameDuration;
				loopCount = frameNumber + 1;
			}

		}
		return keyFrames[frameNumber];
	}
}
